package com.junzhiit.nengji.shop.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.bainaeco.framework.util.DateUtil;
import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.GoodsHistoryDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.cache.GoodsHistoryCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Goods;
import com.junzhiit.nengji.model.GoodsHistory;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.shop.service.GoodsHistoryService;
import com.junzhiit.nengji.shop.web.exception.OrdersNotFoundException;
import com.junzhiit.nengji.shop.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.shop.web.exception.UserNotFoundException;
import com.junzhiit.nengji.shop.web.vo.GoodsHistoryAEVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

@Service
public class GoodsHistoryServiceImpl extends BaseServiceImpl implements GoodsHistoryService {
	
	@Autowired
	private GoodsHistoryDAO goodsHistoryDAO;
	
	@Autowired
	private GoodsHistoryCacheDAO goodsHistoryCacheDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Override
	public ResultVO add(GoodsHistoryAEVO goodsHistoryAEVO) {
		ResultVO result =new ResultVO();
		
		Goods goods= goodsDAO.query(goodsHistoryAEVO.getGoodsId());
		if(goods==null){
			throw new OrdersNotFoundException("订单没找到");
		}
		
		Shop shop= shopDAO.query(goodsHistoryAEVO.getShopId());
		if(shop==null){
			throw new ShopNotFoundException("店铺没找到");
		}
		
		User user=userDAO.query(goodsHistoryAEVO.getUserId());
		if(user==null){
			throw new UserNotFoundException("用户没找到");
		}
		
		String key= DIYKey.getGoodsHistoryKey(DateUtil.getCurrentDateStr(DateUtil.DATE_PATTERN_YYYYMMDD));
		String strList= goodsHistoryCacheDAO.getString(key);
		
		List<GoodsHistoryAEVO> list=new ArrayList<>();
		if(StringUtils.isNotBlank(strList)){
			list=JSON.parseArray(strList, GoodsHistoryAEVO.class);
		}
		
		Map<String, GoodsHistoryAEVO> map=list.stream().collect(Collectors.toMap(GoodsHistoryAEVO::getKey, GoodsHistoryAEVO -> GoodsHistoryAEVO));
		String mapKey=goodsHistoryAEVO.getKey();
		if(map.containsKey(mapKey)){
			map.get(mapKey).setNumber(map.get(mapKey).getNumber()+goodsHistoryAEVO.getNumber());
		}else{
			map.put(mapKey, goodsHistoryAEVO);
		}
		
		Collection<GoodsHistoryAEVO> collection= map.values();
		goodsHistoryCacheDAO.addString(key,JsonUtil.toJson(collection));
		return result;
	}

	@Override
	@Transactional
	public ResultVO add(List<GoodsHistoryAEVO> list) {
		ResultVO result =new ResultVO();
		
		String key= DIYKey.getGoodsHistoryKey(DateUtil.getCurrentDateStr(DateUtil.DATE_PATTERN_YYYYMMDD));
		String strList= goodsHistoryCacheDAO.getString(key);
		
		List<GoodsHistoryAEVO> goodsHistoryAEVOList=new ArrayList<>();
		if(StringUtils.isNotBlank(strList)){
			goodsHistoryAEVOList=JSON.parseArray(strList, GoodsHistoryAEVO.class);
		}
		
		Map<String, GoodsHistoryAEVO> map=goodsHistoryAEVOList.stream().collect(Collectors.toMap(GoodsHistoryAEVO::getKey, GoodsHistoryAEVO -> GoodsHistoryAEVO));
		
		for (GoodsHistoryAEVO goodsHistoryAEVO : list) {
			String mapKey=goodsHistoryAEVO.getKey();
			if(map.containsKey(mapKey)){
				map.get(mapKey).setNumber(map.get(mapKey).getNumber()+goodsHistoryAEVO.getNumber());
			}else{
				map.put(mapKey, goodsHistoryAEVO);
			}
		}

		Collection<GoodsHistoryAEVO> collection= map.values();
		goodsHistoryCacheDAO.addString(key, JsonUtil.toJson(collection));	
		return result;
	}

	@Override
	@Transactional
	public ResultVO add() {
		ResultVO result =new ResultVO();
	
		String date= DateUtil.format(DateUtil.addDays(new Date(),-1), DateUtil.DATE_PATTERN_YYYYMMDD);
		
		String key= DIYKey.getGoodsHistoryKey(date);
		String strList= goodsHistoryCacheDAO.getString(key);
		
		if(StringUtils.isNotBlank(strList)){
			List<GoodsHistory> list=JSON.parseArray(strList, GoodsHistory.class);
			if(list!=null&&list.size()>0){
				goodsHistoryDAO.batchInsert(list);
			}
		}
		
		return result;
	}
	
}
